package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.service.db.AnnouncementService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public PageBeanVo findAnnouncements(Integer pageNumber, Integer pageSize) {

        //1.开启分页
        IPage iPage = new Page();
        //2.查询数据
        iPage = announcementMapper.findAnnouncementsByPage(iPage);
        //System.out.println("从数据库中查询的公告的数据为----" + iPage);
        //3.构建返回值
        PageBeanVo vo = new PageBeanVo(pageNumber, pageSize, iPage.getTotal(), iPage.getRecords());
       // System.out.println("从数据库中查询的公告vo的数据为数据为----" + vo);
        List<Announcement> items = (List<Announcement>) vo.getItems();
        List<AnnouncementVo> announcementList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Announcement announcement : items) {
                AnnouncementVo announcementVo = new AnnouncementVo();
                BeanUtil.copyProperties(announcement, announcementVo);
                announcementVo.setCreateDate(DateUtil.format(announcement.getCreated(),"yyyy-MM-dd HH:mm"));
                System.out.println(announcementVo.getCreateDate());
                announcementList.add(announcementVo);
            }
        }
        vo.setItems(announcementList);
        return vo;

    }
}
